package br.com.honorato.bean;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import org.springframework.security.core.userdetails.UsernameNotFoundException;

import br.com.honorato.ejb.DomainEJB;
import br.com.honorato.ejb.UserEJB;
import br.com.honorato.enun.EUserAppStatus;
import br.com.honorato.enun.EYesNo;
import br.com.honorato.exception.EJBServiceException;
import br.com.honorato.exception.EncriptException;
import br.com.honorato.model.DAdminLogin;
import br.com.honorato.model.DAdminPass;
import br.com.honorato.model.UserApp;
import br.com.honorato.util.Hashing;

@ManagedBean(name = "appScopedBean", eager = true)
@ApplicationScoped
public class AppScopedBean implements Serializable, Runnable
{
	private static final long serialVersionUID = 1L;

	private UserApp userAdminDefault;

	@EJB
	private UserEJB userEJB;

	@EJB
	private DomainEJB domainEJB;

	public AppScopedBean()
	{
		Thread t1 = new Thread(this);
		t1.start();
	}

	public UserApp getUserAdminDefault()
	{
		return userAdminDefault;
	}

	public void setUserAdminDefault(UserApp userAdminDefault)
	{
		this.userAdminDefault = userAdminDefault;
	}

	@Override
	public void run()
	{
		int cont = 0;

		while (((domainEJB == null) || (userEJB == null)))
		{
			cont++;

			if (cont < 100)
			{
				try
				{
					Thread.sleep(1000);
				}
				catch (InterruptedException e)
				{
				}
			}
			else
			{
				// /TODO LOGAR ERRO
			}
		}
		carregarConfiguracaoAdministrador();
		initializeLoginAdminDefault();
	}

	private void initializeLoginAdminDefault()
	{
		String loginAdminDefault = null;
		try
		{
			loginAdminDefault = domainEJB.getLoginAdminDefault().getCode();
			userAdminDefault = userEJB.getUserByLogin(loginAdminDefault);
		}
		catch (EJBServiceException e)
		{
			userAdminDefault = null;
			// TODO loggar erro
		}
	}

	private void carregarConfiguracaoAdministrador()
	{
		UserApp adminDefault = null;

		DAdminLogin loginAdminDefault = null;
		DAdminPass passAdminDefault = null;

		String user = "admin";
		String pass = "123456";

		try
		{
			loginAdminDefault = domainEJB.getLoginAdminDefault();

			if (loginAdminDefault == null)
			{
				loginAdminDefault = new DAdminLogin(user, "Usuario administrador padrao", null, "Administrador do Sistema", 0);
				domainEJB.saveDAdminLogin(loginAdminDefault);
			}

			passAdminDefault = domainEJB.getPassAdminDefault();
			if (passAdminDefault == null)
			{
				passAdminDefault = new DAdminPass(pass, "Senha administrador padrao", null, "Senha do Sistema", 0);
				domainEJB.saveDAdminPass(passAdminDefault);
			}

			adminDefault = userEJB.getUserByLogin(loginAdminDefault.getCode());

			if (adminDefault == null)
			{
				UserApp userApp = new UserApp(null, loginAdminDefault.getCode(), Hashing.toMD5Hashing(passAdminDefault.getCode()));
				userApp.setName(loginAdminDefault.getName());
				userApp.setStatus(EUserAppStatus.ACTIVE);
				userApp.setChangePassword(EYesNo.YES);
				adminDefault = userEJB.saveUser(userApp);
			}
		}
		catch (EJBServiceException | EncriptException ex)
		{
			// TODO BUNDLE
			throw new UsernameNotFoundException("Erro ao recuperar configuracao default de usuario.");
		}
	}
}